
/* 当前充电模块状态信息查询应答/上报 */
const _ = require('lodash');
exports.run = (info, core, socket, logger) => {
  if (!socket.charging) { return; }
  const reader = new core.BufferReader(info.data),
    userIdBuf = reader.read(2),
    cmdIndexBuf = reader.read(2),
    chargingGunCount = reader.read(1);
  const Charging = core.getModel('Charging');
  let data = {
    chargingGunCount: chargingGunCount.readUInt8(),
    chargingGuns: [],
    accessed_at: _.now(),
  };
  _.times(data.chargingGunCount, () => {
    let type = reader.read(1),
        actuatorStatus = reader.read(1),
        moduleId = reader.read(8),
        batteryBms = reader.read(8),
        voltage = reader.read(2),
        electricity = reader.read(2),
        outputPower = reader.read(2),
        moduleErrorCode = reader.read(2),
        status = reader.read(1);
    data.chargingGuns.push({
      type: (() => {
        switch(type.readUInt8()) {
          case 1: return '直流';
          case 2: return '交流';
          default: return '';
        }
      })(),
      actuatorStatus: ((byte) => {
        let status = { gun: '', connector: '' };
        if ((byte >> 0) % 2) {
          status['gun'] = '推入不到位';
        }
        if ((byte >> 1) % 2) {
          status['gun'] = '推入到位';
        }
        switch(`${((byte >> 4) % 2)}${((byte >> 5) % 2)}`) {
          case '00': status['connector'] = '温度正常'; break;
          case '01': status['connector'] = '过温'; break;
          case '10': status['connector'] = '不可信'; break;
        }
        return status;
      })(actuatorStatus[0]),
      moduleId: moduleId.toString('hex'),
      moduleErrorCode: moduleErrorCode.toString('hex'),
      batteryBms: batteryBms.toString('hex'),
      voltage: voltage.readUInt16LE() * 0.1,
      electricity: electricity.readUInt16LE() * 0.1 ,
      outputPower: outputPower.readUInt16LE() * 0.1,
      status: (() => {
        switch(status.readUInt8()) {
          case 0: return '停机';
          case 1: return '充电';
          case 9: return '预约锁定';
          default: return '';
        }
      })(),
    });
  });
  Charging.savePacket(socket.charging, '0x34', data);
}

